package m.base.service;

import m.base.model.GoodsInfo;
import m.base.util.GoodsParam;
import m.base.util.ZolUtil;
import m.common.model.util.ModelQueryList;
import m.common.model.util.ModelUpdateUtil;
import m.common.model.util.QueryCondition;
import m.common.service.Service;
import m.system.exception.MException;
import manage.model.StatusModel;

import java.sql.SQLException;
import java.util.List;

public class GoodsInfoService extends Service {
    public GoodsInfo doSave(Integer id) throws SQLException, MException {
        List<GoodsInfo> ls=ModelQueryList.getModelList(GoodsInfo.class,new String[]{"*"},null, QueryCondition.eq("oid",id));
        if(ls.isEmpty()){
            GoodsInfo model=new GoodsInfo();
            model.setOid(String.valueOf(id));
            model.setStatus(StatusModel.Status_Disabled);
            ModelUpdateUtil.insertModel(model);
            return model;
        }else{
            return ls.get(0);
        }
    }
    public GoodsInfo doCollection(String goodsOid) throws Exception {
        GoodsInfo model=ModelQueryList.getModel(GoodsInfo.class,goodsOid,"*");
        if(null==model) throw new MException(this.getClass(),"商品信息不存在");
        GoodsParam param= ZolUtil.convertGoodsParam(Integer.valueOf(model.getOid()));
        model.setName(param.getGoodsName());
        model.setFactoryName(param.getFactoryName());
        model.setTypeName(param.getTypeName());
        model.setAttrJson(param.toAttrJson());
        model.setStatus(StatusModel.Status_Enable);
        ModelUpdateUtil.updateModel(model, "name","factoryName","typeName", "attrJson", "status");
        return model;
    }

}
